package com.coocaa.x.provider.xprovider.blacklist;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.FirstStart;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.provider.ProviderData;
import com.coocaa.x.provider.xprovider.IProviderHandler;

/**
 * Created by lu on 15-12-22.
 */
public class BlackListProviderHandler implements IProviderHandler {
    public static final BlackListProviderHandler handler = new BlackListProviderHandler();

    private static final String SP_KEY = "list";
    private SharedPreferences blacklist_sp = null;


    private BlackListProviderHandler() {

    }

    @Override
    public boolean onCreate(final Context c) {
        blacklist_sp = c.getSharedPreferences("blacklist", Context.MODE_PRIVATE);
        new Thread() {
            @Override
            public void run() {
                CoocaaApplication.waiting4me();
                if (FirstStart.isFirstStart(c, "xprovider.blacklist", FirstStart.ACTION.FIRST_TODAY)) {
                    FirstStart.markFirstStartFlag(c, "xprovider.blacklist");
                    update();
                }
            }
        }.start();
        return true;
    }

    @Override
    public String getType(Uri uri) throws ProviderHandlerNotHandledException {
        throw new ProviderHandlerNotHandledException(uri);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) throws ProviderHandlerNotHandledException {
        synchronized (blacklist_sp) {
            String value = "";
            try {
                value = blacklist_sp.getString(SP_KEY, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ProviderData.toValueCursor(value);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) throws ProviderHandlerNotHandledException {
        throw new ProviderHandlerNotHandledException(uri);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) throws ProviderHandlerNotHandledException {
        throw new ProviderHandlerNotHandledException(uri);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) throws ProviderHandlerNotHandledException {
        update();
        return 0;
    }

    private void update() {
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                try {
                    LogUtils.d("BL", "update blacklist");
                    String[] newlist = getNewBlackList();
                    if (newlist == null)
                        newlist = new String[0];
                    String value = JSONObject.toJSONString(newlist);
                    synchronized (blacklist_sp) {
                        blacklist_sp.edit().putString(SP_KEY, value).commit();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String[] getNewBlackList() {
        try {
            return CoocaaApplication.getCoocaaWebService().getAppStoreService().getBlackList("");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
